package com.hpjz.common.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.log4j.Log4j;

import org.springframework.stereotype.Service;

import com.hpjz.common.ParameterBean;

import cryptix.jce.provider.MD5;

@Log4j
@Service
public class CommUtils {
	public String CommSet(ParameterBean pb) {
		log.info("11");
		// ResultBean smr = new ResultBean();
		try {

		} catch (Exception e) {
			log.error("CommUtils转换出错:", e);
		}
		return "";
	}

	/**
	 * 字符加密
	 * 
	 * @param str
	 *            需要加密的字符
	 * @return 密码串
	 */
	public static String stringTOMD5(String str) {
		MD5 md = new MD5();
		return md.toMD5(str);
	}

	/**
	 * 获取UUID
	 * 
	 * @return
	 */
	public static String getUUID() {
		String uuid = UUID.randomUUID().toString();
		uuid = uuid.replace("-", "");
		return uuid;
	}
	
	public static HashMap<String, Object> getServletRequest(HttpServletRequest request){
		HashMap<String, Object> map = new HashMap<>();
		Map<String, String[]> params = request.getParameterMap();
		for (String key : params.keySet()) {
			String[] values = params.get(key);
			for (int i = 0; i < values.length; i++) {
				String value = values[i];
				map.put(key, value);
			}
		}
		return map;
	}
	public static String request(String httpUrl, String httpArg) {
		BufferedReader reader = null;
		String result = null;
		StringBuffer sbf = new StringBuffer();
		httpUrl = httpUrl + "?" + httpArg;

		try {
			URL url = new URL(httpUrl);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setRequestMethod("GET");
			connection.setRequestProperty("apikey",
					"4d8773d6894774bac3d02d3522c4ac6b");
			connection.connect();
			InputStream is = connection.getInputStream();
			reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String strRead = null;
			while ((strRead = reader.readLine()) != null) {
				sbf.append(strRead);
				sbf.append("\r\n");
			}
			reader.close();
			result = sbf.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
